package leetcode.code082;

import leetcode.helper.tree.ListNode;

public class Solution extends Solution082 {

	@Override
	public ListNode deleteDuplicates(ListNode head) {
		if (head == null || head.next == null)// 没有或者只有一个头结点
			return head;
		ListNode p1 = head;
		ListNode p2 = p1.next;
		while (p2 != null && head.val == p2.val) {// cut head
			int temp = p2.val;
			do {
				p2 = p2.next;
			} while (p2 != null && p2.val == temp);
			if (p2 == null)// 都切没了
				return null;
			head = p2;
			p1 = head;
			p2 = p1.next;
		}
		while (p2 != null) {
			int temp = p2.val;
			int count = 0;
			do {
				p2 = p2.next;
				count++;
			} while (p2 != null && p2.val == temp);
			if (count == 1) {
				p1 = p1.next;
			} else {
				p1.next = p2;
				if (p2 == null)
					break;
				p2 = p1.next;
			}
		}
		return head;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
	}

}
